package com.zxcl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxcl.domain.Notice;
import com.zxcl.dto.NoticeQueryDTO;
import com.zxcl.dto.PageDTO;
import com.zxcl.mapper.NoticeMapper;
import com.zxcl.service.NoticeService;
import com.zxcl.utils.AuthUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 描述: []
 * 作者: zxcl
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService{

    /**
     * 分页查询公告
     *
     * @param pageDTO
     * @param noticeQueryDTO
     * @return
     */
    @Override
    public Page<Notice> loadNoticePage(PageDTO pageDTO, NoticeQueryDTO noticeQueryDTO) {
        Page<Notice> page=new Page<>(pageDTO.getCurrent(),pageDTO.getSize());
        Long shopId = AuthUtil.getShopId();
        return getBaseMapper().selectPage(page,new LambdaQueryWrapper<Notice>()
                .eq(shopId!=0L,Notice::getShopId,shopId)
                .eq(!ObjectUtils.isEmpty(noticeQueryDTO.getStatus()),Notice::getStatus,noticeQueryDTO.getStatus())
                .eq(!ObjectUtils.isEmpty(noticeQueryDTO.getIsTop()),Notice::getIsTop,noticeQueryDTO.getIsTop())
                .like(StringUtils.hasText(noticeQueryDTO.getTitle()),Notice::getTitle,noticeQueryDTO.getTitle())
                .orderByDesc(Notice::getIsTop,Notice::getCreateTime)
        );
    }

    /**
     * 新增公告
     *
     * @param notice
     * @return
     */
    @Override
    public int addNotice(Notice notice) {
        Long shopId = AuthUtil.getShopId();
        notice.setShopId(shopId);
        notice.setUpdateTime(new Date());
        notice.setCreateTime(new Date());
        return getBaseMapper().insert(notice);
    }

    /**
     * 查询商城首页的公告
     */
    @Override
    public List<Notice> findMallNotices() {
        return getBaseMapper().selectList(new LambdaQueryWrapper<Notice>()
                .select(Notice::getTitle)
                .eq(Notice::getStatus,1)
                .orderByDesc(Notice::getCreateTime)
        );
    }


    /**
     * 查询商城首页的公告详情列表
      * @return
     */
    @Override
    public List<Notice> findMallNoticeList() {
        return getBaseMapper().selectList(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getStatus,1)
                .orderByDesc(Notice::getCreateTime)
        );
    }
}
